from django.shortcuts import render

from rest_framework.generics import ListAPIView
from .models import CourseCategory
from .serializers import CourseCategoryModelSerializer


class CourseCategoryListAPIView(ListAPIView):
    """
    课程分类表
    """
    queryset = CourseCategory.objects.filter(is_delete=False, is_show=True).order_by("orders", "-id")
    serializer_class = CourseCategoryModelSerializer


from .serializers import CourseModelSerializer
from .models import Course
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.filters import OrderingFilter
from .paginations import CourseListPagination


class CourseListAPIView(ListAPIView):
    """
    课程表
    """
    queryset = Course.objects.filter(is_show=True, is_delete=False).order_by('orders', '-id')
    serializer_class = CourseModelSerializer
    filter_backends = [DjangoFilterBackend, OrderingFilter]
    filter_fields = ['course_category']  # 过滤方式
    ordering_fields = ['id', 'students', 'price']  # 按照默认 ，人气，价格排序
    pagination_class = CourseListPagination  # 分页信息


from rest_framework.generics import RetrieveAPIView
from .serializers import CourseRetrieveModelSerializer


class CourseRetrieveAPIView(RetrieveAPIView):
    """课程详情"""
    queryset = Course.objects.filter(is_delete=False, is_show=True)
    serializer_class = CourseRetrieveModelSerializer


from .models import CourseChapter
from .serializers import CourseChapterModelSerializer


class ChapterListAPIView(ListAPIView):
    """课程章节"""
    queryset = CourseChapter.objects.filter(is_show=True, is_delete=False).order_by("chapter")
    serializer_class = CourseChapterModelSerializer
    filter_backends = [DjangoFilterBackend]
    filter_fields = ['course'] #按当前课程分类


